﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace pageAnalyzer
{
    public partial class BufferScroll : Form
    {
        private Queue<string> buffer;
        //Thread deamonT=null;
        Form1 form1;
        public BufferScroll(Queue<string> b)
        {
            this.buffer = b;
            InitializeComponent();
            this.closed = false;
            this.FormClosed += new FormClosedEventHandler(BufferScroll_FormClosed);
            
        }

        MemoryStream ms;

        public BufferScroll(Form1 f)
        {
            this.form1 = f;
            InitializeComponent();
            this.closed = false;
            this.buffer = form1.buffer;
            this.ms = form1.ms;
            this.FormClosed += new FormClosedEventHandler(BufferScroll_FormClosed);
            this.textBox1.BackColor = Color.BlueViolet;
            this.textBox1.ForeColor = Color.Cyan;
        }

        void BufferScroll_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.closed = true;
            if (this.timer1.Enabled)
            {
                this.timer1.Stop();
            }
            if (this.form1.t != null)
            {
                this.form1.t.Abort(); ;
            }
        }
        private bool closed = false;
        public void WriteData()
        {
            //while (!this.closed)
            //{
                //if (buffer.Count > 0)
                //{
                //    if (this.textBox1.Text.Length > 10000)
                //    {
                //        this.textBox1.Text = this.textBox1.Text.Substring(5000);
                //    }
                //    lock(buffer)
                //    {
                //        this.textBox1.AppendText(buffer.Dequeue());
                //    }
                //}
                
                //Thread.Sleep();
            //}
            this.timer1.Interval=500;
            this.timer1.Enabled = true;
            this.timer1.Tick += new EventHandler(timer1_Tick);
            
            this.timer1.Start();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            //lock (ms)
            //{
            //    long backupPosition = ms.Position;
            //    ms.Position = 0;
            //    this.textBox1.Text = new StreamReader(ms, Encoding.Unicode).ReadToEnd();

            //    ms.Position = backupPosition;
            //}


            if (buffer.Count > 0)
            {
                if (this.textBox1.Text.Length > 100000)
                {
                    this.textBox1.Text = this.textBox1.Text.Substring(50000);
                }
                lock (buffer)
                {
                    while (buffer.Count > 0)
                    {
                        this.textBox1.AppendText(buffer.Dequeue());
                    }
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            WriteData();
        }


    }
}
